//---------------------------------------------------------------------
// File: EntLibDatabaseRowCountStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System;
using System.Xml;
using BizUnit.Extensions.EntLib.Utilities;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
    /// <summary>
    /// The EntLibDatabaseRowCountStep step uses the supplied configuration file, database configuration name, table name and condition to determine the number of rows in the database
    /// and validates this against the expected rows.
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.EntLibDatabaseRowCountStep">
    ///		<ConfigFileLocation>..\\..\\db.config</ConfigFileLocation>
    ///		<DatabaseConfigName>Northwind</DatabaseConfigName>
    ///		<Table>table</Table>
    ///		<Condition>id=1</Condition>
    ///		<ExpectedRows>100</ExpectedRows>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>ConfigFileLocation</term>
    ///			<description>The config file containing the Enterprise Library configuration for db access</description>
    ///		</item>
    ///     <item>
    ///			<term>DatabaseConfigName</term>
    ///			<description>The config section name of the databased used in the test</description>
    ///		</item>
    ///		<item>
    ///			<term>Table</term>
    ///			<description>Table name select rows from</description>
    ///		</item>
    ///		<item>
    ///			<term>Condition</term>
    ///			<description>Condition to use</description>
    ///		</item>
    ///		<item>
    ///			<term>ExpectedRows</term>
    ///			<description>The number of expected rows</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class EntLibDatabaseRowCountStep : ITestStep
    {
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
        {
            string configFileLocation = context.ReadConfigAsString(testConfig, "ConfigFileLocation");
            string databaseConfigName = context.ReadConfigAsString(testConfig, "DatabaseConfigName");
            string table = context.ReadConfigAsString(testConfig, "Table");
            string condition = context.ReadConfigAsString( testConfig, "Condition" );
            int expectedRows = context.ReadConfigAsInt32( testConfig, "ExpectedRows" );

            // Build SQL statement
            string sqlStatement = "select count(*) from " + table + " where " + condition ;

            context.LogInfo( "EntLibDatabaseRowCountStep connecting to {0}, executing statement {1}", databaseConfigName, sqlStatement ) ;

            // Execute command against specified database
            int rows = EntLibDatabaseHelper.ExecuteScalar( configFileLocation, databaseConfigName, sqlStatement ) ;

            // Number of rows as expected?
            if ( rows != expectedRows )
            {
                throw new ApplicationException( string.Format( "EntLibDatabaseRowCountStep failed, expected {0} rows but found {1} rows", expectedRows, rows ) ) ;
            }
            else
            {
                context.LogInfo( "EntLibDatabaseRowCountStep found \"{0}\" rows", rows ) ;
            }	// input
        }
	}
}
